/**
 *  Description
 *  @class
 * @classdesc selle klassi objekt hoiab ning töötleb sensorite andmeid sel viisil, kui nad on vajalikud FlotChart teegi API jaoks. Selle meetodid võimaldavad joonte andmeid lisada, eemaldada ning ümber joonistada HTML Canvas’el.
 * 
 */
var Graph = function(data){
	var _this = this;
	this.xCenter = 0;
	this.data = data;
	this.ticker;
	this.nihe = 0;
	this.SITO_TIHEDUS = 600;
	this.pikkus = 1000;
	this.series = [];
	this.options = {
            yaxis: { min:this.data.korgMin, max:this.data.korgMax, tickSize:this.data.tickY() },
            xaxis: {show: false},
           // xaxis: {min:0, max:100, tickSize: 10}
           grid: { markings:  [ { color: "#000", lineWidth: 1, xaxis: { from: this.xCenter-1, to: this.xCenter+1} }] }
	};
	this.barOptions = {
            yaxis: { min:1000, max:80000, tickSize:10000 },
            //  xaxis: {show: false},
              xaxis: {min:0, max:50, tickSize: 10}
    };
	this.colors =  ["#1B7B71", "#7B6C1B", "#E6C617", "#AD17E6", "#E64617", "#1EE617", "#E61799", "#646D6C", "#420FEF", "#9999EE", "#00AA88",  "#FF0000"];
	this.load = function(e){
		_this.data.fromFile(e);
	//	_this.setVer();
		_this.draw();
	};
	this.draw = function (shift, xtrmums){
        this.data.tickY();
        var opts = this.options;
        this.series = [];
        var curvBxs = $("#curveControls :checkbox[name=curve]:checked");
        var hetk = this.data.getTime();
        if(shift) { hetk+=shift;}
        for (var i = 0; i < curvBxs.length; i++){
                  var fields=curvBxs[i].id.split("_");
                  switch(this.getType()){
                       case "lines":
                            this.series.push({
                            	data: this.extractCurve(curvBxs[i].id.split("_")[1], curvBxs[i].id.split("_")[0], hetk, this.pikkus),
                            	lines: {show: true}, 
                            	color: this.colors[curvBxs[i].id.split("_")[2]]
                            });
                            break;
                       case "bars":
                            opts= this.barOptions;
                            this.barOptions.xaxis.tickSize = 10;
                            this.series.push({
                            	data: this.extractBar(curvBxs[i].id.split("_")[1], curvBxs[i].id.split("_")[0], hetk+1700+this.nihe), 
                            	bars: {show: true}, 
                            	color: this.colors[curvBxs[i].id.split("_")[2]]
                            });
                            break;
                      default: 
                            $("#content").append("ERROR");
             }
        }
        $.plot($("#placeholder"), this.series, opts);
        if (isNaN(this.data.getTime())||isNaN(this.data.startCam))  $("#subplot").text("0 ms");
        else $("#subplot").text(this.data.getTime()-this.data.startCam+" ms");
	};
	this.extractCurve = function(sensor, fys, start, pikkus){
		 var ret=[];
         var offset = Math.floor((start-this.data.parsed[0]["absolute timestamp"][0])/this.data.sagedus-pikkus/2-this.nihe); //akvalang
         var sito=Math.round(pikkus/this.SITO_TIHEDUS);
         if (sito<1) sito=1;
        this.xCenter = offset+pikkus/2-0.5;
        this.options.grid.markings[0].xaxis.to=this.xCenter;
        this.options.grid.markings[0].xaxis.from=this.xCenter;
         for (var i= offset; i < offset+pikkus ; i++){
              if (i%sito!=0 && this.data.xtrms[sensor][fys][i]==null) continue;// V"AHENDAME ITERATSIOONIDE ARVU SITO KORDA
              //var position = startCam-0+20*(i-offset);
              ret.push([i, this.data.parsed[sensor][fys][i]]);
         }
         return ret;
	};
	this.extractBar = function(snsr, fys, htk){
		var ret=[];
        this.barOptions.xaxis.show=true;
        this.barOptions.xaxis.min = 0;
        this.barOptions.yaxis.min = this.data.korgMin;
        this.barOptions.xaxis.max = 50;
        this.barOptions.yaxis.max = this.data.korgMax;
        iter = Math.floor((htk-this.data.startCam)/this.data.sagedus+this.nihe);
        ret.push([10+this.series.length*5, this.data.parsed[snsr][fys][iter]]);
        return ret;
	};
	this.skaleerHoriz = function(x){
		var zoomIn=x<0;
        if (this.pikkus <3 && zoomIn) return;
        if (this.pikkus <=19 && zoomIn) x=-1;
        else if (this.pikkus <=110 && zoomIn) x=-10;
        if (this.pikkus <=10 && !zoomIn) x=1;
        else if (this.pikkus <=100 && !zoomIn) x=10;
        this.pikkus +=x;
        $("#graafHald.pikkus").text(graph.pikkus);
        this.draw();
	};
	this.skaleerHorCycle = function(x){
			_this.ticker= setInterval(function (){
             _this.skaleerHoriz(x);
         }, 100);
	};
	this.skalVerMax = function(y){
		if (this.data.korgMax+y <= this.data.korgMin-y) return;
			this.data.korgMax +=y;
			$("#dataHald.korgMax").text(this.data.korgMax);
			this.options.yaxis.max=this.data.korgMax;
			this.options.yaxis.tickSize=this.data.tickY();
			this.draw();
	};
	this.skalVerMin = function(y){
		 if (this.data.korgMin-y >= this.data.korgMax+y) return;
		 	this.data.korgMin -=y;
		 	$("#dataHald.korgMin").text(this.data.korgMin);
		 	this.options.yaxis.min=this.data.korgMin;
		 	this.draw();
	};
	this.setVerMax = function(){
		//alert(this.data.korgMax);
		this.options['yaxis']['max']=this.data.korgMax; 
		this.options['yaxis']['tickSize']=this.data.tickY(); 
	};
    this.setVerMin = function(){ 
    	this.options['yaxis']['min']=this.data.korgMin; 
    	this.options['yaxis']['tickSize']=this.data.tickY();
    };
    this.setVer = function(){
    	this.setVerMax();
    	this.setVerMin(); 
    };
    this.skaleerHorRelease = function(){ 
    	 clearInterval(_this.ticker);
    };
    this.skalVerCycle = function(y){
         _this.ticker = setInterval(function(){
              _this.skalVerMax(y);
              _this.skalVerMin(y)
         }, 100);
    };
    this.getType = function(){ return $("#chooseGraph :radio:checked").val(); };
    

	
};